package com.example.messageboard.controller;

import com.example.messageboard.model.MessageInfo;
import com.example.messageboard.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-13
 * Time: 16:55
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    //定义一个集合来存储留言信息
//    private List<MessageInfo> messageInfos = new ArrayList<>();
//改用数据库来存储信息
    @Autowired
    private MessageService messageService;
    @RequestMapping("/publish")
    public boolean publish(MessageInfo messageInfo){
        System.out.println("打印publish日志"+messageInfo);
        //1:参数校验，存储结果
        if(!StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
//        messageInfos.add(messageInfo);
        Integer result = messageService.publishMessage(messageInfo);
        if(result > 0){
            return true;
        }
        return false;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        System.out.println("打印getList日志");
//        return messageInfos;
        return messageService.getMessageList();
    }
}
